package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectServiceImpl implements ICollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private StrategyDetailMapper detailMapper;
    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private CollectCommentMapper collectCommentMapper;

    @Override
    public void save(Collect collect) {
        if (collect.getTravelId() != null) {
            Collect c = collectMapper.selectByTravelId(collect.getTravelId());
            if (c == null) {
                collectMapper.insert(collect);
            }else {
                collectMapper.deleteTravel(collect.getTravelId());

            }

        }

        if(collect.getStrategyId() != null){
            Collect c = collectMapper.selectByStrategyId(collect.getStrategyId());
            if (c == null) {
                collectMapper.insert(collect);
            }else {
                collectMapper.deleteStrategy(collect.getStrategyId());
            }
        }

        if(collect.getDailyId() != null){
            Collect c = collectMapper.selectByDailyId(collect.getDailyId());
            if (c == null) {
                collectMapper.insert(collect);
            }else {
                collectMapper.deleteDaily(collect.getDailyId());
            }
        }


    }

    @Override
    public Map<String, Object> list(Long userId) {
        List<Collect> list = collectMapper.list(userId);
        //使用集合分别储存收藏的游记,攻略,日报等
        List<Travel> travels = new ArrayList<>();
        List<StrategyDetail> details = new ArrayList<>();
        List<Daily> dailies = new ArrayList<>();

        for (Collect collect : list) {
            //根据是否有收藏查询对应的收藏内容
            if (collect.getTravelId() != null) {
                Travel travel = travelMapper.selectByPrimaryKey(collect.getTravelId());
                travels.add(travel);
            }
            if (collect.getStrategyId() != null) {
                StrategyDetail detail = detailMapper.selectByPrimaryKey(collect.getStrategyId());
                details.add(detail);
            }
            if (collect.getDailyId() != null) {
                Daily daily = dailyMapper.selectByPrimaryKey(collect.getDailyId());
                dailies.add(daily);
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("travels", travels);
        map.put("details", details);
        map.put("dailies", dailies);
        return map;
    }

    @Override
    public Collect select(Collect collect) {

        return collectMapper.select(collect);
    }

    //查询某篇收藏文章的评论
    @Override
    public List<CollectComment> listComment(CollectComment collectComment) {

        return collectCommentMapper.listComment(collectComment);
    }
    //保存谋篇文章的评论
    @Override
    public void saveComment(CollectComment collectComment) {
        collectCommentMapper.insert(collectComment);
    }
}
